package main

import "fmt"

type Monkey struct {
	name string
	age int
}

/**
空接口接受任意值
*/
func emptyInterface() {
	dict := make(map[string]interface{})
	dict["a"] = 1
	dict["b"] = "b"
	dict["c"] = true
	fmt.Println(dict)
	fmt.Printf("type: %T, value: %v\n", dict["b"], dict["b"])
}

/**
空接口实现类型断言
*/
func emptyInterfaceAssert() {
	var x interface{} = "b"
	value, ok := x.(string)
	fmt.Println("value: ", value, "ok: ", ok)
}

/**
空接口实现动态类型判断
*/
func dynamicType(x interface{}) {
	switch v := x.(type) {
	case int:
		fmt.Printf("type: %T, value: %v\n", v, v)
	case string:
		fmt.Printf("type: %T, value: %v\n", v, v)
	case bool:
		fmt.Printf("type: %T, value: %v\n", v, v)
	default:
		fmt.Printf("未知类型\n")
	}
}

/**
空接口实现类型打印
*/
func print(x interface{}) {
	switch v := x.(type) {
	case map[string]int:
		for key, value := range v {
			fmt.Println("key: ", key, "value: ", value)
		}
	case *Monkey:
		fmt.Println("name: ", v.name, "age: ", v.age)
	default:
		fmt.Println("unknown type")
		
	}
}

func main() {
	dict := map[string]int{"a": 1}
	print(dict)
	monkey := Monkey{name: "miss", age: 3}
	print(&monkey)

	emptyInterface()
	emptyInterfaceAssert()

	i := 1
	s := "s"
	b := true
	dynamicType(i)
	dynamicType(s)
	dynamicType(b)
}